<!DOCTYPE html>
<html>
<head>
    <title>医疗智能问答系统</title>
    <meta charset="UTF-8">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;500;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary-color: #4CAF50;
            --secondary-color: #E8F5E9;
            --text-color: #2C3E50;
            --border-radius: 12px;
            --sidebar-width: 250px;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Noto Sans SC', 'Microsoft YaHei', Arial, sans-serif;
            background: url('https://img.freepik.com/free-photo/medical-banner-with-doctor-wearing-coat_23-2149611193.jpg') no-repeat center center fixed;
            background-size: cover;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            color: #2C3E50;
            margin: 0;
            padding: 0;
        }

        .header {
            width: 100%;
            background: linear-gradient(135deg, var(--primary-color), #2196F3);
            padding: 15px 0;
            text-align: center;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            position: fixed;
            top: 0;
            left: 0;
            z-index: 1000;
        }

        h1 {
            color: white;
            font-size: 2.2em;
            font-weight: 700;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
        }

        .main-container {
            width: 90%;
            max-width: 1200px;
            margin: auto;
            padding: 0;
            display: flex;
            background: rgba(255, 255, 255, 0.8);
            border-radius: var(--border-radius);
            box-shadow: 0 8px 30px rgba(0,0,0,0.12);
            border: 1px solid rgba(255,255,255,0.3);
            height: 85vh;
            margin-top: calc(73px + 2vh);
        }

        .sidebar {
            width: var(--sidebar-width);
            padding: 30px 20px;
            display: flex;
            flex-direction: column;
            border-right: 1px solid rgba(0,0,0,0.1);
        }

        .sidebar-header {
            padding-bottom: 15px;
            border-bottom: 1px solid rgba(0,0,0,0.1);
            margin-bottom: 15px;
        }

        .sidebar-header h2 {
            font-size: 1.2em;
            color: var(--text-color);
            margin-bottom: 10px;
        }

        .new-chat-btn {
            width: 100%;
            padding: 10px;
            background: var(--primary-color);
            color: white;
            border: none;
            border-radius: var(--border-radius);
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 14px;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }

        .new-chat-btn:hover {
            background: #43A047;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(76,175,80,0.3);
        }

        .new-chat-btn:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .new-chat-btn svg {
            width: 16px;
            height: 16px;
            fill: currentColor;
        }

        .send-btn {
            padding: 12px;
            width: 45px;
            height: 45px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: var(--primary-color);
            color: white;
            border: none;
            border-radius: var(--border-radius);
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .send-btn svg {
            width: 20px;
            height: 20px;
            fill: currentColor;
        }

        .send-btn:hover {
            background: #43A047;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(76,175,80,0.3);
        }

        .send-btn:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .history-list {
            flex: 1;
            overflow-y: auto;
        }

        .history-item {
            padding: 10px;
            margin-bottom: 8px;
            border-radius: var(--border-radius);
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 14px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            display: flex;
            justify-content: space-between;
            align-items: center;
            background: rgba(255, 255, 255, 0.1);
        }

        .history-item:hover {
            background: rgba(0,0,0,0.05);
        }

        .history-item.active {
            background: rgba(232, 245, 233, 0.8);
            color: var(--primary-color);
        }

        .delete-chat {
            padding: 4px 8px;
            background: transparent;
            color: #666;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
            min-width: auto;
            opacity: 0;
            transition: all 0.3s ease;
        }

        .history-item:hover .delete-chat {
            opacity: 1;
        }

        .delete-chat:hover {
            background: rgba(255,0,0,0.1);
            color: #ff4444;
            transform: none;
            box-shadow: none;
        }

        .chat-container {
            flex: 1;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            padding: 20px 0;
        }

        .chat-box {
            height: calc(80vh - 140px);
            overflow-y: auto;
            padding: 40px;
            background: transparent;
            display: flex;
            flex-direction: column;
            justify-content: flex-start;
        }

        .message-container {
            width: 100%;
            margin-bottom: 10px;
        }

        .message {
            display: inline-block;
            max-width: 90%;
            word-break: break-all;
            white-space: pre-wrap;
            font-size: 16px;
            line-height: 1.7;
            padding: 12px 16px;
            border-radius: 12px;
            background: #e8f5e9;
            color: #2C3E50;
        }

        .user-message {
            background: #4CAF50;
            color: #fff;
            float: right;
        }

        .bot-message {
            background: #e8f5e9;
            color: #2C3E50;
            float: left;
        }

        .input-container {
            display: flex;
            padding: 20px;
            border-top: 1px solid rgba(0,0,0,0.1);
            gap: 10px;
            background: transparent;
        }

        #questionInput {
            flex: 1;
            padding: 15px;
            border: 2px solid #e0e0e0;
            border-radius: var(--border-radius);
            font-size: 15px;
            transition: all 0.3s ease;
            font-family: inherit;
        }

        #questionInput:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(76,175,80,0.2);
        }

        /* 自定义滚动条 */
        .chat-box::-webkit-scrollbar,
        .history-list::-webkit-scrollbar {
            width: 8px;
        }

        .chat-box::-webkit-scrollbar-track,
        .history-list::-webkit-scrollbar-track {
            background: #f1f1f1;
            border-radius: 4px;
        }

        .chat-box::-webkit-scrollbar-thumb,
        .history-list::-webkit-scrollbar-thumb {
            background: #888;
            border-radius: 4px;
        }

        .chat-box::-webkit-scrollbar-thumb:hover,
        .history-list::-webkit-scrollbar-thumb:hover {
            background: #666;
        }

        /* 打字机效果的光标 */
        .typing::after {
            content: '|';
            animation: blink 1s infinite;
        }

        @keyframes blink {
            0%, 100% { opacity: 1; }
            50% { opacity: 0; }
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .main-container {
                width: 100%;
                height: calc(100vh - 73px);  /* 减去header高度 */
                margin-top: 73px;  /* header height */
                padding: 0;
                flex-direction: column;
            }

            .sidebar {
                width: 100%;
                margin-bottom: 0;
                border-right: none;
                border-bottom: 1px solid rgba(0,0,0,0.1);
            }

            .chat-box {
                height: calc(100vh - 200px);  /* 调整移动端的高度 */
            }

            .message {
                max-width: 85%;
            }
        }

        /* 添加背景遮罩效果 */
        body::before {
            display: none;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>医疗智能问答系统</h1>
    </div>
    <div class="main-container">
        <div class="sidebar">
            <div class="sidebar-header">
                <button class="new-chat-btn" onclick="newChat()">
                    <svg viewBox="0 0 24 24">
                        <path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
                    </svg>
                    新建对话
                </button>
            </div>
            <div class="history-list" id="historyList">
                <!-- 历史记录将通过JavaScript动态添加 -->
            </div>
        </div>
        <div class="chat-container">
            <div class="chat-box" id="chatBox"></div>
            <div class="input-container">
                <input type="text" id="questionInput" placeholder="请输入您的医疗相关问题...">
                <button class="send-btn" onclick="askQuestion()">
                    <svg viewBox="0 0 24 24">
                        <path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z"></path>
                    </svg>
                </button>
            </div>
        </div>
    </div>

    <script>
        let currentChatId = null;
        let chatHistory = [];

        function newChat() {
            currentChatId = Date.now().toString();
            chatHistory.push({
                id: currentChatId,
                title: '新对话',
                messages: []
            });
            updateHistoryList();
            document.getElementById('chatBox').innerHTML = '';
            addMessage('您好，我是小李医药智能助理，请问有什么可以帮您？', false);
        }

        function updateHistoryList() {
            const historyList = document.getElementById('historyList');
            historyList.innerHTML = '';
            
            chatHistory.forEach(chat => {
                const item = document.createElement('div');
                item.className = `history-item ${chat.id === currentChatId ? 'active' : ''}`;
                
                const title = document.createElement('span');
                title.textContent = chat.title;
                title.onclick = () => loadChat(chat.id);
                
                const deleteBtn = document.createElement('button');
                deleteBtn.className = 'delete-chat';
                deleteBtn.innerHTML = '删除';
                deleteBtn.onclick = (e) => {
                    e.stopPropagation();
                    deleteChat(chat.id);
                };
                
                item.appendChild(title);
                item.appendChild(deleteBtn);
                historyList.appendChild(item);
            });
        }

        function loadChat(chatId) {
            currentChatId = chatId;
            const chat = chatHistory.find(c => c.id === chatId);
            if (chat) {
                const chatBox = document.getElementById('chatBox');
                chatBox.innerHTML = '';
                chat.messages.forEach(msg => {
                    addMessage(msg.text, msg.isUser);
                });
                updateHistoryList();
            }
        }

        function addMessage(message, isUser) {
            const chatBox = document.getElementById('chatBox');
            const container = document.createElement('div');
            container.className = 'message-container';

            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${isUser ? 'user-message' : 'bot-message'}`;
            messageDiv.textContent = message;

            container.appendChild(messageDiv);
            chatBox.appendChild(container);

            // 确保滚动到底部
            setTimeout(() => { chatBox.scrollTop = chatBox.scrollHeight; }, 100);
        }

        function askQuestion() {
            const input = document.getElementById('questionInput');
            const question = input.value.trim();
            const sendButton = document.querySelector('.send-btn');
            
            if (question) {
                input.disabled = true;
                sendButton.disabled = true;
                
                addMessage(question, true);
                input.value = '';

                // 创建EventSource连接
                const eventSource = new EventSource(`/ask?question=${encodeURIComponent(question)}&stream=true`);
                let answer = '';
                let botMessageDiv = null; // 用于拼接流式内容

                eventSource.onmessage = function(event) {
                    const data = JSON.parse(event.data);
                    if (data.answer) {
                        answer += data.answer;
                        // 只在第一次收到片段时新建消息
                        if (!botMessageDiv) {
                            // 新建消息节点
                            const chatBox = document.getElementById('chatBox');
                            const container = document.createElement('div');
                            container.className = 'message-container';
                            botMessageDiv = document.createElement('div');
                            botMessageDiv.className = 'message bot-message';
                            botMessageDiv.textContent = answer;
                            container.appendChild(botMessageDiv);
                            chatBox.appendChild(container);
                            // 滚动到底部
                            setTimeout(() => { chatBox.scrollTop = chatBox.scrollHeight; }, 50);
                        } else {
                            // 后续片段直接拼接
                            botMessageDiv.textContent = answer;
                            // 只滚动主聊天区，不滚动小窗口
                            const chatBox = document.getElementById('chatBox');
                            chatBox.scrollTop = chatBox.scrollHeight;
                        }
                    }
                };

                eventSource.onerror = function(error) {
                    console.error('EventSource failed:', error);
                    eventSource.close();
                    input.disabled = false;
                    sendButton.disabled = false;
                    input.focus();
                };

                eventSource.onclose = function() {
                    input.disabled = false;
                    sendButton.disabled = false;
                    input.focus();
                };
            }
        }

        function deleteChat(chatId) {
            const index = chatHistory.findIndex(c => c.id === chatId);
            if (index > -1) {
                chatHistory.splice(index, 1);
                if (chatId === currentChatId) {
                    if (chatHistory.length > 0) {
                        loadChat(chatHistory[0].id);
                    } else {
                        newChat();
                    }
                } else {
                    updateHistoryList();
                }
            }
        }

        document.getElementById('questionInput').addEventListener('keypress', function(e) {
            if (e.key === 'Enter' && !this.disabled) {
                askQuestion();
            }
        });

        window.onload = function() {
            newChat(); // 自动创建新对话
        };
    </script>
</body>
</html> 